-- Grow-a-Garden 宠物信息获取模块
-- 包含名称、重量、年龄、稀有度等完整信息

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

-- 获取宠物数据模块
local function getPetDataModule()
    local success, PetData = pcall(function()
        return require(ReplicatedStorage.Modules.GardenGuideModules.DataModules.PetData)
    end)
    if success and PetData and PetData.Data then
        return PetData.Data
    end
    return nil
end

-- 解析宠物名称，提取基础名称（去除重量和年龄信息）
local function parsePetBaseName(fullName)
    if not fullName then return nil end
    -- 提取基础名称，例如 "Bunny [11.85 KG] [Age 80]" -> "Bunny"
    local baseName = fullName:match("^([^%[]+)")
    return baseName and baseName:gsub("%s+$", "") or nil
end

-- 解析名称中的重量/年龄
local function parseWeightAge(name)
    local w = tonumber((name or ""):match("%[(%d+%.%d+)%sKG%]"))
    local a = tonumber((name or ""):match("%[Age%s(%d+)%]"))
    return w, a
end

-- 获取宠物的完整信息
local function getPetInfo(tool)
    if not tool or not tool:IsA("Tool") then
        return nil
    end
    
    local fullName = tool.Name
    local baseName = parsePetBaseName(fullName)
    local weight, age = parseWeightAge(fullName)
    
    -- 获取稀有度
    local rarity = nil
    local petData = getPetDataModule()
    if petData and baseName then
        rarity = petData[baseName] and petData[baseName].Rarity
    end
    
    -- 如果从模块获取不到，尝试从Tool属性获取
    if not rarity then
        rarity = tool:GetAttribute("Rarity") or tool.Rarity
    end
    
    return {
        fullName = fullName,
        baseName = baseName,
        weight = weight,
        age = age,
        rarity = rarity,
        tool = tool
    }
end

-- 获取背包中所有宠物的信息
local function getAllPetsInfo()
    local pets = {}
    local backpack = LocalPlayer:FindFirstChild("Backpack")
    
    if backpack then
        for _, tool in ipairs(backpack:GetChildren()) do
            if tool:IsA("Tool") then
                local petInfo = getPetInfo(tool)
                if petInfo then
                    table.insert(pets, petInfo)
                end
            end
        end
    end
    
    -- 检查手中的宠物
    local character = LocalPlayer.Character
    if character then
        local equippedTool = character:FindFirstChildOfClass("Tool")
        if equippedTool then
            local petInfo = getPetInfo(equippedTool)
            if petInfo then
                table.insert(pets, petInfo)
            end
        end
    end
    
    return pets
end

-- 根据稀有度筛选宠物
local function getPetsByRarity(rarity)
    local allPets = getAllPetsInfo()
    local filteredPets = {}
    
    for _, pet in ipairs(allPets) do
        if pet.rarity == rarity then
            table.insert(filteredPets, pet)
        end
    end
    
    return filteredPets
end

-- 打印宠物信息（用于调试）
local function printPetInfo(petInfo)
    if not petInfo then return end
    
    print(string.format("宠物: %s", petInfo.fullName))
    print(string.format("  基础名称: %s", petInfo.baseName or "未知"))
    print(string.format("  重量: %s KG", petInfo.weight or "未知"))
    print(string.format("  年龄: %s", petInfo.age or "未知"))
    print(string.format("  稀有度: %s", petInfo.rarity or "未知"))
    print("---")
end

-- 导出宠物信息到JSON文件
local function exportPetsInfo()
    local pets = getAllPetsInfo()
    local HttpService = game:GetService("HttpService")
    
    local success, jsonData = pcall(function()
        return HttpService:JSONEncode(pets)
    end)
    
    if success then
        writefile("pets_info.json", jsonData)
        print("宠物信息已导出到 pets_info.json")
    else
        print("导出失败: " .. tostring(jsonData))
    end
end

-- 测试函数
local function testPetInfo()
    print("=== 测试宠物信息获取 ===")
    local pets = getAllPetsInfo()
    
    for _, pet in ipairs(pets) do
        printPetInfo(pet)
    end
    
    print(string.format("总共找到 %d 个宠物", #pets))
end

-- 返回模块
return {
    getPetInfo = getPetInfo,
    getAllPetsInfo = getAllPetsInfo,
    getPetsByRarity = getPetsByRarity,
    printPetInfo = printPetInfo,
    exportPetsInfo = exportPetsInfo,
    testPetInfo = testPetInfo
}
